package com.jt.controller;

import com.google.gson.Gson;
import com.jt.pojo.User;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/hello")
@Controller
public class HelloController {

    @RequestMapping(value = "HelloController",method = RequestMethod.GET)
    public ModelAndView Hello(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("msg","你好 96969");
        return modelAndView;
    }

    @RequestMapping(value = "getUser",method = RequestMethod.GET)
    public String getUser(ModelMap modelMap){
        modelMap.addAttribute("msg","9999999999");
        return "index";
    }

    @RequestMapping(value = "UserController",method = RequestMethod.GET)
    public String ll(ModelMap modelMap){
        modelMap.addAttribute("msg","88888888");
        return "index";
    }
    @ResponseBody
    @RequestMapping(value = "addUser")
    public String addUser(String name){
        System.out.println(name);
        return name;
    }
    // 设置返回的是响应内容(文本)
    @ResponseBody
    @RequestMapping("/ajax")
    public String ajax(String username){
        System.out.println("ajax");
        //ajax 是什么 用来做什么
        // 异步请求
        // 异步请求通过什么返回  response.write();

        return username;
    }

    @RequestMapping("/upload")
    // 上传文件的时候需要备注文件的name值
    public String upload(@RequestPart("file")MultipartFile multipartFile){
        File file = new File("G:\\669\\"+multipartFile.getOriginalFilename());
        try {
            InputStream is = multipartFile.getInputStream();
            OutputStream os = new FileOutputStream(file);
            IOUtils.copy(is,os);
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "index";
    }

    @RequestMapping("/download")
    public String download(HttpServletResponse response){

        //既然要下载文件

        // 1：先准备一个文件

        File file = new File("G:\\669\\oppo.jpg");
        //设置回复的类型
        response.setContentType("application/x-msdownload");
        //名称 当文件名是中文的时候需要转码
        String filename ="";
        try {
            filename = URLEncoder.encode(file.getName(),"GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-Disposition", "attachment;filename="+file.getName());
        try {
            FileCopyUtils.copy(new FileInputStream(file),response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "index";
    }

    @RequestMapping("/ajax")
    public String json(){

        List<User> list = new ArrayList<User>();
        for (int i=0;i<50;i++){
            User user = new User();
            user.setName("aaa"+i);
            user.setPassword(i+"aaa"+i);
            user.setSex(i+"a");
            user.setAge(i);
            user.setSex("男"+i);
            user.setImg("img"+i);
            user.setStatus(i);
            list.add(user);
        }
        Gson gson = new Gson();
        return gson.toJson(list);
    }


}
